package com.example.t9_2;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.widget.Button;
import android.widget.DatePicker;
import android.view.View.OnClickListener;
import android.app.DatePickerDialog;


public class MainActivity extends Activity {

	private TextView nowdate;
	private Button setdate;
	private int year;
	private int month;
	private int day;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nowdate=(TextView)this.findViewById(R.id.time);
        setdate=(Button)this.findViewById(R.id.b1);
        Calendar c=Calendar.getInstance(Locale.CHINA);
        Date mydate=new Date();
        c.setTime(mydate);
        year=c.get(Calendar.YEAR);
        month=c.get(Calendar.MONTH);
        day=c.get(Calendar.DAY_OF_MONTH);
        nowdate.setText(year+"-"+(month+1)+"-"+day);
        setdate.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		DatePickerDialog dpd=new DatePickerDialog(MainActivity.this,Datelistener,year,month,day);
        		dpd.show();
        	}});}
    private DatePickerDialog.OnDateSetListener Datelistener=new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker v, int y, int m, int d) {
			 Calendar ca=Calendar.getInstance(Locale.CHINA);
		        Date md=new Date();
		        ca.setTime(md);
		        year=ca.get(Calendar.YEAR);
		        month=ca.get(Calendar.MONTH);
		        day=ca.get(Calendar.DAY_OF_MONTH);
			if(year>=y&&month>=m&&day>=d)
			{
				year=y;
				month=m;
				day=d;
				updateDate();

			
			}
			else
			{
				Builder dialog=new AlertDialog.Builder(MainActivity.this);
		        dialog.setIcon(null);
		        dialog.setTitle("error");
		        dialog.setMessage("date error");
		        dialog.setPositiveButton("no", null);
		        dialog.setNegativeButton("yes", null);
		        dialog.create();
		        dialog.show();
			}
		}
		private void updateDate(){
			nowdate.setText(year+"-"+(month+1)+"-"+day);
			
		}
	};


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
